//
//  GlobalImports.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

@_exported import Foundation
@_exported import UIKit
@_exported import SnapKit

import AVFoundation

public func k_Height_statusBar() -> CGFloat {
    var statusBarHeight: CGFloat = 0;
    if #available(iOS 13.0, *) {
        let scene = UIApplication.shared.connectedScenes.first;
        guard let windowScene = scene as? UIWindowScene else {return 0};
        guard let statusBarManager = windowScene.statusBarManager else {return 0};
        statusBarHeight = statusBarManager.statusBarFrame.height;
    } else {
        statusBarHeight = UIApplication.shared.statusBarFrame.height;
    }
    return statusBarHeight;
}
/// ②、顶部安全区高度 k_Height_safeAreaInsetsTop
public func k_Height_safeAreaInsetsTop() -> CGFloat {
    if #available(iOS 13.0, *) {
        let scene = UIApplication.shared.connectedScenes.first;
        guard let windowScene = scene as? UIWindowScene else {return 0}; // guard：如果 expression 值计算为false，则执行代码块内的 guard 语句。(必须包含一个控制语句: return、 break、 continue 或 throw。)。as?：类型转换，(还有这两种：as、as!)
        guard let window = windowScene.windows.first else {return 0};
        return window.safeAreaInsets.top;
    } else if #available(iOS 11.0, *) {
        guard let window = UIApplication.shared.windows.first else {return 0};
        return window.safeAreaInsets.top;
    }
    return 0;
}

/// ③、底部安全区高度
public func k_Height_safeAreaInsetsBottom() -> CGFloat {
    if #available(iOS 13.0, *) {
        let scene = UIApplication.shared.connectedScenes.first;
        guard let windowScene = scene as? UIWindowScene else {return 0};
        guard let window = windowScene.windows.first else {return 0};
        return window.safeAreaInsets.bottom;
    } else if #available(iOS 11.0, *) {
        guard let window = UIApplication.shared.windows.first else {return 0};
        return window.safeAreaInsets.bottom;
    }
    return 0;
}
/** 屏幕宽度 */
public let k_ScreenWidth = UIScreen.main.bounds.size.width
/** 屏幕高度 */
public let k_ScreenHeight = UIScreen.main.bounds.size.height
/* 导航栏高度 固定高度 = 44.0f */
//let k_Height_NavContentBar :CGFloat  = UINavigationBar.appearance().frame.size.height
public let k_Height_NavBar :CGFloat = 44.0
/** 状态栏高度 */
public let k_Height_StatusBar :CGFloat = k_Height_statusBar()
/** 状态栏+导航栏的高度 */
public let k_Nav_Height: CGFloat = k_Height_NavBar + k_Height_StatusBar
/** 底部tabBar栏高度（不包含安全区，即：在 iphoneX 之前的手机） */
public let k_TabBar_Height :CGFloat = 49.0
/** 底部导航栏高度（包括安全区），一般使用这个值 */
public let k_Height_TabBar :CGFloat = k_Height_safeAreaInsetsBottom() + k_TabBar_Height



// MARK: - 检查系统版本 -
/* 检查系统版本 */
/// 版本号相同：
public func systemVersionEqual(version:String) -> Bool {
    return UIDevice.current.systemVersion == version
}

/// 系统版本高于等于该version  测试发现只能传入带一位小数点的版本号  不然会报错    具体原因待探究
public func systemVersionGreaterThan(version:String) -> Bool {
    return UIDevice.current.systemVersion.compare(version, options: .numeric, range: version.startIndex..<version.endIndex, locale: Locale(identifier:version)) != ComparisonResult.orderedAscending
}

//判断是否是 x、及x以上 系列
public func isIphoneX() -> Bool {
    return k_Height_safeAreaInsetsBottom() > 0.0; // 底部安全区 > 0 时，
    
}

//设计稿为 375*812 比例,所以比例系数为 375.0/kScreenWidth
public let kDesignScale = k_ScreenWidth/375.0


public func kGetUIH(designSize:CGSize,currentW:CGFloat) -> CGFloat {
    let scale = designSize.width/currentW
    return designSize.height/scale
}
    

public func debugPrint<T>(_ messsage: T, file: String = #file, funcName: String = #function, lineNum: Int = #line) {
    #if DEBUG
    let fileName = (file as NSString).lastPathComponent
    print(Date.hmsString + " \(fileName) (\(funcName)): [\(lineNum)]- \(messsage)")
    #endif
}

public func dePrint<T>(_ messsage: T) {
    #if DEBUG
    print(Date.hmsString + " \(messsage)")
    #endif
}

public func className(from object: Any) -> String {
    return String(describing: type(of: object))
}

public func appShortVersion() ->String{
    let short = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
    return short
}

public func appBundleID() -> String {
    return Bundle.main.object(forInfoDictionaryKey: "CFBundleIdentifier") as? String ?? ""
}

public func appVersion() ->String{
    let short = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
    return "V" + short
}

/// 震动
public func playVibration() {
    // 默认震动   kSystemSoundID_Vibrate
    // 1519     短震 3D Touch中的peek震动反馈
    // 1520     短震 3D Touch中的pop震动反馈
    // 1521     连续三次短震动
    // peek的震动反馈轻于pop
    
    let soundID: UInt32 = 1520
    AudioServicesPlaySystemSound(soundID)
}

public func kPresentModalVC(target:UIViewController,
                     modelVC:UIViewController,
                            needNewNav:Bool = true,
                            style:UIModalPresentationStyle = .fullScreen,//.overFullScreen,
             transitionStyle:UIModalTransitionStyle = .coverVertical,
                  completion: (() -> Void)? = nil){
    
    if needNewNav {
        let navi = TSBaseNavigationC(rootViewController: modelVC)
        navi.modalPresentationStyle = style
        navi.modalTransitionStyle = transitionStyle
        target.present(navi, animated: true,completion: completion)
    }else{
        modelVC.modalPresentationStyle = style
        modelVC.modalTransitionStyle = transitionStyle
        target.present(modelVC, animated: true,completion: completion)
    }
}

public func kPushVC(target:UIViewController,modelVC:UIViewController, animated: Bool = true){
    modelVC.hidesBottomBarWhenPushed = true
    target.navigationController?.pushViewController(modelVC, animated: animated)
}

public func kGetVideoThumbnail(from videoURL: URL) -> UIImage? {
    let asset = AVAsset(url: videoURL)
    let assetImgGenerate = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    
    let time = CMTimeMakeWithSeconds(0.0, preferredTimescale: 600) // 获取第0秒的帧
    do {
        let imgRef = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
        return UIImage(cgImage: imgRef)
    } catch {
        print("Error generating thumbnail: \(error.localizedDescription)")
        return nil
    }
}
